package org.answer.controller;

import com.sun.org.apache.regexp.internal.RE;
import org.answer.constant.MessageConstant;
import org.answer.entity.Result;
import org.answer.pojo.Menu;
import org.answer.pojo.Role;
import org.answer.service.MenuService;

import org.answer.utils.DateUtils;
import org.answer.utils.ErrorMailUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;

import org.apache.dubbo.config.annotation.Reference;
import org.apache.ibatis.annotations.Param;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 菜单管理
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;
    //log4j日志对象
    private Logger logger = ErrorMailUtils.getLogger();
    /**
     * 根据用户名查询可见的菜单
     * @用户名 username
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(String username) {
        try {
            List<Menu> list = menuService.findAll(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, list);
        } catch (Exception e) {
            logger.error("异常发生时间：" + DateUtils.getCurrentTime(), e);
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    /**
     * 根据角色id查询可见的菜单id
     * @角色id id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            List<Integer> list = menuService.findById(id);
            return new Result(true, "成功", list);
        } catch (Exception e) {
            logger.error("异常发生时间：" + DateUtils.getCurrentTime(), e);
            return new Result(false, "失败");
        }
    }

    /**
     * 修改角色可见菜单
     * @可见菜单id数组 ids
     * @角色对象 role
     * @return
     */
    @RequestMapping("/updateById")
    public Result updateById(Integer[] ids, @RequestBody Role role) {
        Integer id = role.getId();
        try {
            menuService.updateById(id, ids);
            return new Result(true, "成功");
        } catch (Exception e) {
            logger.error("异常发生时间：" + DateUtils.getCurrentTime(), e);
            return new Result(false, "修改可见菜单失败");
        }
    }
}
